# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=lemmy-ui
pkgver=0.19.5
# this needs to be synced with lemmy
_translations_commit=972942ea4ee4281d6941a5d3bb3b0edbb7017297
pkgrel=0
pkgdesc="Link aggregator and forum for the Fediverse - Official webapp"
url="https://join-lemmy.org/"
# armhf, armv7, ppc64le, s390x, x86: follow lemmy aport
# riscv64: argument 'src' must be a string
arch="aarch64 loongarch64 x86_64"
license="AGPL-3.0-only"
depends="nodejs"
makedepends="
	glib-dev
	npm
	pnpm
	python3
	vips-dev
	"
checkdepends="
	cmd:start-stop-daemon
	curl
	"
install="$pkgname.pre-install"
pkgusers="lemmy-ui"
pkggroups="lemmy-ui"
subpackages="$pkgname-openrc"
source="https://github.com/LemmyNet/lemmy-ui/archive/$pkgver/lemmy-ui-$pkgver.tar.gz
	https://github.com/LemmyNet/lemmy-translations/archive/$_translations_commit/lemmy-translations-$pkgver.tar.gz
	lemmy-ui.confd
	lemmy-ui.initd
	"
options="net" # tests access localhost

# use system libvips
export SHARP_FORCE_GLOBAL_LIBVIPS=1

prepare() {
	default_prepare

	rmdir -v lemmy-translations
	ln -sv "$srcdir/lemmy-translations-$_translations_commit" \
		lemmy-translations

	sed -i "s/unknown version/$pkgver/" src/shared/version.ts

	# -U_FORTIFY_SOURCE: "error: inlining failed in call to 'always_inline'
	# 'vsnprintf': function body can be overwritten at link time"
	local extra_flags="-U_FORTIFY_SOURCE $(pkgconf --cflags glib-2.0)"
	export CFLAGS="$CFLAGS $extra_flags"
	export CPPFLAGS="$CPPFLAGS $extra_flags"
	export CXXFLAGS="$CXXFLAGS -std=c++17 $extra_flags"

	# Add modules needed to build sharp with system libvips
	pnpm add node-addon-api@8.0.0 node-gyp@10.1.0

	pnpm install --frozen-lockfile
}

build() {
	export GIT_DIR="$builddir"
	NODE_ENV=production pnpm build:prod
	unset GIT_DIR

	# Remove sharp for linux (glibc), leaving sharp for linuxmusl
	find node_modules -name "*sharp-*linux-*" -exec rm -vrf '{}' \+
}

check() {
	local tmpdir="${tmpdir:-"$(mktemp -d)"}"
	mkdir -p "$tmpdir"

	start-stop-daemon -Sbmp "$tmpdir"/test.pid -w 5000 \
		-e 'NODE_ENV=production' -x node dist/js/server.js
	curl -o "$tmpdir"/test.out http://127.0.0.1:1234 || true
	start-stop-daemon -Kp "$tmpdir"/test.pid
	grep -q "UI: $pkgver" "$tmpdir"/test.out
}

package() {
	mkdir -p "$pkgdir"/usr/share/webapps/$pkgname
	cp -r dist node_modules "$pkgdir"/usr/share/webapps/$pkgname/

	# cleanup unused files
	find "$pkgdir" -type f -a \( \
		-name "*.ts" \
		-o -name "webpack*" \
		-o -name "tsconfig*" \
		-o -name "babel.config*" \
		-o -name "README*" \
		-o -name "readme*" \
		-o -name "CHANGELOG*" \
		-o -name "*.map" \
		-o -name "LICENSE*" \
		-o -name "License" \
		-o -name "license" \
		-o -name "*.md" \
		\) \
		-delete
	find "$pkgdir" -type d -a \( \
		-name "example" \
		-o -name "examples" \
		-o -name "test" \
		-o -name "tests" \
		-o -name ".github" \
		\) \
		-exec rm -rf '{}' \+

	install -Dm644 "$srcdir"/lemmy-ui.confd "$pkgdir"/etc/conf.d/lemmy-ui
	install -Dm755 "$srcdir"/lemmy-ui.initd "$pkgdir"/etc/init.d/lemmy-ui
}

sha512sums="
615bcb836815aa3e336153f0795d160d17059123532ec6b17d479e68d0c29d1d26f7a49060cb19013e0df50798319b29470b18fb24841b3dda7e3d3575808e13  lemmy-ui-0.19.5.tar.gz
838b7b51a2651eb416765f8c5e462ad9ad913672f60597c6e280f470f71143d859ddbe7174b907ff710925d3b0537a6a5d4cb447787e90d3c23a1485818209ce  lemmy-translations-0.19.5.tar.gz
cac55baba8fa7cd49e41877e80296b1505aee35444d025c6c612a5c7091f36c92adeab17117d8f58d228f7bbe9f016a9e752a8b9c0f9a940bd8980132577e1a9  lemmy-ui.confd
59559ffab27cd9562b0f920a72df0a66676f96612960c573132ef1d8914d16ff36300e2514c0fd8e3d16ffd13af58fc129afc88b14f64a16468457e224b97fbd  lemmy-ui.initd
"
